package xyz.weizhi.tanhua.appserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.weizhi.tanhua.appserver.interceptor.UserHolder;
import xyz.weizhi.tanhua.appserver.service.SettingsService;
import xyz.weizhi.tanhua.appserver.service.UserService;
import xyz.weizhi.tanhua.model.domain.Settings;
import xyz.weizhi.tanhua.model.vo.PageResult;
import xyz.weizhi.tanhua.model.vo.SettingsVo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author milian
 * @Date 2021/10/24 0024 16:23
 */
@RestController
@RequestMapping("/users")
public class SettingsController {
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private UserService userService;
    /*
        获取用户通用设置
     */
    @GetMapping("/settings")
    public ResponseEntity Settings(){
        SettingsVo settings = settingsService.settings();
        return ResponseEntity.ok(settings);
    }
    /*
        设置陌生人问题
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        String content = (String) map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }
    /*
        通知设置
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map){
        settingsService.updateSetting(map);
        return ResponseEntity.ok(null);
    }

    /*
        黑名单-翻页列表
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int pagesize  ){
        PageResult blacklist = settingsService.blacklist(page, pagesize);
        return ResponseEntity.ok(blacklist);
    }

    /*
        取消黑名单
     */
    @DeleteMapping("/blacklist/:uid")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId){
        settingsService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }
    /*
        修改手机号--发送手机验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        // 获取用户手机号
        String mobile = UserHolder.getMobile();
        // 发送手机验证码
        userService.sendMsg(mobile);
        return ResponseEntity.ok(null);
    }

    /*
    修改手机号--校验验证码
    */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        // 获取用户手机号
        String mobile = UserHolder.getMobile();
        // 发送手机验证码
        boolean verification = userService.checkVerificationCode(mobile, (String)map.get("verificationCode"));
        Map<String, Boolean> map1 = new HashMap();
        map1.put("verification",verification);
        return ResponseEntity.ok(map1);
    }
    /*
        修改手机号--保存
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map){
        settingsService.updatePhone((String) map.get("phone"));
        return ResponseEntity.ok(null);
    }
    /*
        更新头像
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(MultipartFile file) throws IOException {
        settingsService.updateHeader(file);
        return ResponseEntity.ok(null);
    }
}
